package de.openlaboratory.elib.controller.servlets;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.openlaboratory.elib.view.ViewConfiguration;

/**
 * 
 * http://docs.jboss.org/hibernate/stable/core.old/reference/en/html/tutorial-
 * webapp.html
 * 
 * The pattern we are applying here is called session-per-request. When a
 * request hits the servlet, a new Hibernate Session is opened through the first
 * call to getCurrentSession() on the SessionFactory. Then a database
 * transaction is started-all data access as to occur inside a transaction, no
 * matter if data is read or written (we don't use the auto-commit mode in
 * applications). Do not use a new Hibernate Session for every database
 * operation. Use one Hibernate Session that is scoped to the whole request. Use
 * getCurrentSession(), so that it is automatically bound to the current Java
 * thread.
 * 
 * @author Yogi
 * 
 */
public abstract class GenericDataServlet extends HttpServlet
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1485932506186686283L;
	
	protected Map<String, String> messages;
	ViewConfiguration configuration = new ViewConfiguration();

	protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException;
//	{
//		try
//		{
//			// Begin unit of work
//			HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
//
//			renderPage(response);
//
//			// End unit of work
//			HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
//
//		}
//		catch (Exception ex)
//		{
//			HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
//			throw new ServletException(ex);
//		}
//	}

//	private void renderPage(HttpServletResponse response)
//			throws IOException
//	{
//		PrintWriter printWriter;
//
//	    response.setContentType("text/html");
//	    printWriter = response.getWriter();
//	    printWriter.println("<html><body>");
//	    printWriter.print("This Servlet does not generate any output.");
//	    printWriter.println("</body></html>");
//		
//	}

	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo()
	{
		return "Generic Data Servlet.";
	}
}
